﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace UI.teacher
{
    public partial class MyTask : Form
    {
        public int pageSize = 10;     //每页显示行数
        public int pageCount = 0;    //页数＝总记录数/每页显示行数
        public int pageCurrent = 0;   //当前页号

        public int startIndex = 1;
        public int endIndex = 10;

        public int LoginId = 0;
        public MyTask()
        {
            InitializeComponent();
        }

        private void PageIndex()
        {
            if (pageCurrent == 1)
            {
                startIndex = 1;
                endIndex = startIndex + pageSize;
            }
            else
            {
                startIndex = endIndex+1;
                endIndex = startIndex + pageSize;
            }
        }

        private void TaskAuction_Load(object sender, EventArgs e)
        {
            this.LoginId = Help.UserInfo.User.E_Id;
            if (this.LoginId == 0)
            {
                MessageBox.Show("你还没有登录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            load_TaskIng();
            Event();
        }

        private void Event()
        {
            //职位
            this.tabControl1.SelectedIndexChanged += new EventHandler(tabControl1_SelectedIndexChanged);
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.tabControl1.SelectedIndex == 0)
            {

                load_TaskIng();
            }
            else
            {
                load_TaskFinish();
            }
        }

        private void load_TaskIng()
        {
            //正在进行的任务
            XueMeng.BLL.EmpTaskManager em = new XueMeng.BLL.EmpTaskManager();
            int num = em.GetRecordCount("E_Id=" + this.LoginId + " and Enable=0");
            pageCount = (num / pageSize);    //计算出总页数
            if ((num % pageSize) > 0) pageCount++;
            pageCurrent = 1;    //当前页数从1开始
            PageIndex();

            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.RowHeadersVisible = false;
            this.dataGridView1.DefaultCellStyle.BackColor = Color.White;
            DataSet ds = new DataSet();

            XueMeng.BLL.EmpTaskManager tm = new XueMeng.BLL.EmpTaskManager();

            ds = tm.GetListByPage("E_Id=" + this.LoginId + " and Enable=0", "", startIndex, endIndex);
            DataTable dt = ds.Tables[0];

            List<XueMeng.Model.EmpTask> EmpTask = new List<XueMeng.Model.EmpTask>();
            EmpTask = tm.DataTableToList(dt);

            this.bindingSource1.DataSource = EmpTask;

            this.dataGridView1.DataSource = this.bindingSource1;
            this.bindingNavigator1.BindingSource = this.bindingSource1;
            this.dataGridView1.Columns[0].Visible = false; //任务ID

            toolStripLabel4.Text = pageCount.ToString();
            toolStripTextBox4.Text = Convert.ToString(pageCurrent);
        }

        private void load_TaskFinish()
        {
            //已经完成的任务
            XueMeng.BLL.EmpTaskManager em = new XueMeng.BLL.EmpTaskManager();
            int num = em.GetRecordCount("E_Id=" + this.LoginId + " and Enable=1");
            pageCount = (num / pageSize);    //计算出总页数
            if ((num % pageSize) > 0) pageCount++;
            pageCurrent = 1;    //当前页数从1开始
            PageIndex();

            this.dataGridView2.AutoGenerateColumns = false;
            this.dataGridView2.RowHeadersVisible = false;
            this.dataGridView2.DefaultCellStyle.BackColor = Color.White;
            DataSet ds = new DataSet();

            XueMeng.BLL.EmpTaskManager tm = new XueMeng.BLL.EmpTaskManager();

            ds = tm.GetListByPage("E_Id=" + this.LoginId + " and Enable=1", "", startIndex, endIndex);
            DataTable dt = ds.Tables[0];

            List<XueMeng.Model.EmpTask> EmpTask = new List<XueMeng.Model.EmpTask>();
            EmpTask = tm.DataTableToList(dt);

            this.bindingSource1.DataSource = EmpTask;
            this.dataGridView2.DataSource = this.bindingSource1;
            this.bindingNavigator2.BindingSource = this.bindingSource1;
            this.dataGridView2.Columns[0].Visible = false; //任务ID
            toolStripLabel8.Text = pageCount.ToString();
            toolStripTextBox3.Text = Convert.ToString(pageCurrent);
        }

        private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            xueguan.DataPropertyName = null;
            S_Name.DataPropertyName = null;
            jpfz.DataPropertyName = null;
            bianhao.DataPropertyName = null;

            for (int i = e.RowIndex; i < e.RowIndex + e.RowCount; i++)
            {
                XueMeng.Model.Task ts = new XueMeng.BLL.TaskManager().GetModel((dataGridView1.Rows[i].DataBoundItem as XueMeng.Model.EmpTask).T_Id);
                XueMeng.Model.Employee emp = new XueMeng.BLL.EmployeeManager().GetModel(ts.X_Id);
                XueMeng.Model.Student smp = new XueMeng.BLL.StudentManager().GetModel(ts.S_Id);
                XueMeng.Model.Auction auc = new XueMeng.BLL.AuctionManager().GetModelList("T_Id=" + ts.T_Id + "")[0];
                string fenzhi = ts.Fraction_Sta.ToString() + "-" + ts.Fraction_Sto.ToString();
                dataGridView1.Rows[i].Cells[bianhao.Index].Value = i+1;
                dataGridView1.Rows[i].Cells[xueguan.Index].Value = emp.E_Name; //学管师
                dataGridView1.Rows[i].Cells[S_Name.Index].Value = smp.S_Name; //学生
                dataGridView1.Rows[i].Cells[Fraction.Index].Value = fenzhi;
                dataGridView1.Rows[i].Cells[jpfz.Index].Value = auc.Fraction;
                dataGridView1.Rows[i].Cells[CreateDate.Index].Value = auc.CreateDate;
                dataGridView1.Rows[i].Cells[FinishDate.Index].Value = auc.FinishDate;
            }
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex >= 0)
                {
                    dataGridView1.ClearSelection();
                    dataGridView1.Rows[e.RowIndex].Selected = true;
                    dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                }
            }
        }

        private void dataGridView2_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex >= 0)
                {
                    dataGridView2.ClearSelection();
                    dataGridView2.Rows[e.RowIndex].Selected = true;
                    dataGridView2.CurrentCell = dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex];
                }
            }
        }

        private void dataGridView2_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            xueguan.DataPropertyName = null;
            S_Name.DataPropertyName = null;
            jpfz.DataPropertyName = null;
            bianhao.DataPropertyName = null;

            for (int i = e.RowIndex; i < e.RowIndex + e.RowCount; i++)
            {
                XueMeng.Model.Task ts = new XueMeng.BLL.TaskManager().GetModel((dataGridView2.Rows[i].DataBoundItem as XueMeng.Model.EmpTask).T_Id);
                XueMeng.Model.Employee emp = new XueMeng.BLL.EmployeeManager().GetModel(ts.X_Id);
                XueMeng.Model.Student smp = new XueMeng.BLL.StudentManager().GetModel(ts.S_Id);
                XueMeng.Model.Auction auc = new XueMeng.BLL.AuctionManager().GetModelList("T_Id=" + ts.T_Id + "")[0];
                string fenzhi = ts.Fraction_Sta.ToString() + "-" + ts.Fraction_Sto.ToString();
                dataGridView2.Rows[i].Cells[bianhao.Index].Value = i + 1;
                dataGridView2.Rows[i].Cells[xueguan.Index].Value = emp.E_Name; //学管师
                dataGridView2.Rows[i].Cells[S_Name.Index].Value = smp.S_Name; //学生
                dataGridView2.Rows[i].Cells[Fraction.Index].Value = fenzhi;
                dataGridView2.Rows[i].Cells[jpfz.Index].Value = auc.Fraction;
                dataGridView2.Rows[i].Cells[CreateDate.Index].Value = auc.CreateDate;
                dataGridView2.Rows[i].Cells[FinishDate.Index].Value = auc.FinishDate;
            }
        }

        private void 竞拍此任务ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string T_Id = "";
            try
            {
                T_Id = this.dataGridView1.SelectedCells[0].Value.ToString();
            }
            catch {
                return;
            }
            XueMeng.Model.Task ts = new XueMeng.BLL.TaskManager().GetModel(Convert.ToInt32(T_Id));
            if (ts.T_Id.ToString().Equals(""))
            {
                MessageBox.Show("没有找到相应的任务!","错误");
                return;
            }
            //XueMeng.Model.Student smp = new XueMeng.BLL.StudentManager().GetModel(ts.S_Id);
            //XueMeng.Model.Auction auc = new XueMeng.BLL.AuctionManager().GetModelList("T_Id=" + ts.T_Id + "")[0];
            //DataTable dt = new XueMeng.BLL.ProblemManager().GetList("T_Id = " + ts.T_Id + "").Tables[0];
            //List<XueMeng.Model.Problem> pr = new XueMeng.BLL.ProblemManager().DataTableToList(dt);
            ShowTask b = new ShowTask();
            b.T_Id = Convert.ToInt32(T_Id);
            b.Owner = this;
            b.Show();
        }
    }
}
